<template>
  <div>
    <div class="login_container">
      <div class="login_box">
        <div class="avatar_box">
          <img src="../../assets/change.jpg" alt="">
        </div>
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
          <!--用户名-->
          <el-form-item prop="username">
            <el-input ref="input" v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
          </el-form-item>
          <!--初始密码-->
          <el-form-item prop="primaryPassword">
            <el-input v-model="loginForm.primaryPassword" placeholder="请输入初始密码" prefix-icon="el-icon-lock" type="password" />
          </el-form-item>
          <!--修改密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入修改密码" prefix-icon="el-icon-lock" type="password" @keyup.enter.exact.native="modifyPassword()" />
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="success" @click="modifyPassword(),clickHandler($event)">确认修改</el-button>
            <router-link to="/userLogin" style="float: right; margin-top: 40px; font-size: 16px; ">返回</router-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import userApi from '@/api/user'
export default {
  beforeRouteLeave(to, from, next) {
    if (this.canvasFlag) {
      this.deleteCanvas()
    }
    next()
  },
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: this.$store.getters.name || '',
        primaryPassword: '',
        password: ''
      },
      // 表单验证对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        primaryPassword: [
          { required: true, message: '请输入初始密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入修改密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      },
      canvasFlag: false
    }
  },
  created() {
    this.$moment.init()
    this.$moment.animate()
    this.canvasFlag = true
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.input.focus()
    })
  },
  methods: {
    // 表单实例对象，重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    deleteCanvas() {
      document.getElementById('wall-bg').remove()
      this.canvasFlag = false
    },
    modifyPassword() {
      userApi.modifyPassword(this.loginForm).then(resp => {
        console.log(this.loginForm)
        console.log(resp)
        resp.data.code === 200 ? this.$message.success('密码修改成功!') : this.$message.error('密码修改失败!')
        this.$router.push({ path: '/userLogin' })
      }, err => {
        console.log(err.message)
      })
    }
  }

}
</script>

<style>
.login_container {
    height: 100%;
    margin-top: -730px;
}

.login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 370px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 10px 0 rgb(0 33 79 / 11%);
    transform: translate(-50%,-50%);
}

.login_box .avatar_box {
  position: absolute;
    height: 130px;
    width: 130px;
    border:1px solid #eee;
    border-radius:50%;
    padding:10px;
    box-shadow:0 0 10px  #ddd;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #eee;
}

.login_box .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}

.btns {
    display: flex;
    justify-content: flex-end;
}

.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
